/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.app.local.Localisation;
import fever.app.local.LocalisationEvent;
import fever.app.local.LocalisationListener;
import fever.events.EventPriority;
import fever.exception.IllegalAccessException;
import fever.exception.IllegalArgumentException;
import fever.utils.Stringifier;
import fever.utils.StringUtil;

/**
 * All resources must extends this basic class.
 * 
 * @author Romain Ecarnot
 */
class fever.core.Resources implements LocalisationListener
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _nodeID : String;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/** 
	 * Triggered when Localisation language change.
	 */
	public function onLocalisationUpdate( event : LocalisationEvent ) : Void
	{
		throw new IllegalAccessException( 'must be override' );
	}
	
	/**
	 * Returns translation node ID.
	 */
	public function getNodeID() : String
	{
		return _nodeID;	
	}
	
	/**
	 * Returns a full qualified path for passed-in  translation node 
	 * {@code id}. 
	 */
	public function getFullNodePath( id : String ) : String
	{
		return _nodeID + '.' + id;	
	}
	
	/**
	 * Returns translation for passed-in {@code id}.
	 */
	public function getTranslation( id : String ) : String
	{
		return StringUtil.replace( getResource( id ), '\\n', '\n' );
	}
	
	/**
	 * Returns global resource.
	 */
	public function getResource( id : String )
	{
		return ( this[ id ] != undefined ) 
			? this[ id ] 
			: Localisation.getResource( _nodeID + '.' + id );
	}
	
	/**
	 * Returns passed-in {@code id} node as {@code Boolean} type.
	 */
	public function getBoolean( id : String ) : Boolean
	{
		return Boolean( getResource( id ) );
	}
	
	/**
	 * Returns passed-in {@code id} node as {@code Number} type.
	 */
	public function getNumber( id : String ) : Number
	{
		return Number( getResource( id ) );
	}
	
	/**
	 * Returns passed-in {@code id} node as {@code Array} type.
	 */
	public function getArray( id : String ) : Array
	{
		return Array( getResource( id ) );
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function Resources( id : String )
	{
		if( !id ) 
			throw new IllegalArgumentException( 'localisation node ID must be defined' );
		
		_nodeID = id;
		_initProperties();
	}
	
	private function _initProperties() : Void
	{
		if( Localisation.isLoaded() && Localisation.hasResource( _nodeID ) )
		{
			Localisation.addLocalisationListener( this, EventPriority.LOCALISATION_MANAGMENT );
		}
		else _initDefault();
	}
	
	private function _initDefault() : Void
	{
		throw new IllegalAccessException( 'Resources._initDefault() must be override' );	
	}
}